package io.github.eiot.json.serializer;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import io.github.eiot.codec.ByteBufRef;
import io.netty.buffer.ByteBuf;
import io.vertx.core.json.impl.JsonUtil;

import java.io.IOException;

/**
 * created by wang007 on 2025/10/10
 */
public class ByteBufRefSerializer extends JsonSerializer<ByteBufRef> {

    @Override
    public void serialize(ByteBufRef value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
        ByteBuf byteBuf = value.byteBuf();
        int len = byteBuf.readableBytes();
        byte[] bytes = new byte[len];
        byteBuf.readBytes(bytes);
        gen.writeString(JsonUtil.BASE64_ENCODER.encodeToString(bytes));
    }
}
